package com.people.core.log.config;

import com.people.core.log.aspect.ServerLogAdvice;
import com.people.core.log.properties.ServerLogProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 日志配置
 *
 * @author yuanqinglong
 * @date 2022/7/7 11:12
 */
@Configuration
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "service-log", name = "enabled", havingValue = "true", matchIfMissing = true)
public class LogConfiguration {


    /**
     * 服务器日志属性
     */
    private final ServerLogProperties serverLogProperties;


    /**
     * 日志方面切入点
     *
     * @return {@link AspectJExpressionPointcutAdvisor}
     */
    @Bean
    public AspectJExpressionPointcutAdvisor serviceLogAspectJExpressionPointcutAdvisor() {
        AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
        advisor.setExpression(serverLogProperties.getPointcut());
        advisor.setAdvice(serviceLogAdvice());
        return advisor;
    }


    /**
     * 服务日志建议
     *
     * @return {@link ServerLogAdvice}
     */
    @Bean
    @RefreshScope
    public ServerLogAdvice serviceLogAdvice() {
        return new ServerLogAdvice(serverLogProperties);
    }

}
